using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using way.model;
using way.util;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace way. controller
{
    /**
     * 控制层只接收前端请求，不能被此外的方法调用
     * 作用：维护前后端接口
     */
    public class MenuController
    {
        MenuCore menuCore = InstanceManage.getMenuCore();
        MenuCore messageCore = InstanceManage.getMenuCore();
        IMenuService menuService= InstanceManage.getMenuService();

        public void queryMenu(Result<Dictionary<int, MenuEntity>> res)
        {
            menuService.queryMenu(res);
        }
        public Result<MenuEntity> queryMenuById(Result<MenuEntity> res)
        {
           return menuService.queryMenuById(res);
        }
        public Result<List<MenuEntity>> queryMenuSurroundsById(Result<List<MenuEntity>> res)
        {
            return menuService.queryMenuSurroundsById(res);
        }
        public void sortMenuIdMap(Result<Dictionary<int, MenuEntity>> req, Result<Dictionary<int, List<MenuEntity>>> res)
        {
            res.data = req.data.Values
                .GroupBy(menu => menu.up)
                .ToDictionary(
                    group => group.Key,
                    group => group.OrderBy(menu => menu.sort).ToList()
                );
        }

        public Result<int> menuAddHead(string name, Result<int> res)
        {
           return menuService.menuAddHead(name, res);
        }

        public Result<int> menuAddChild(int targetId, string newName, Result<int> res)
        {
            return menuService.menuAddChild(targetId, newName, res);
        }

        public Result<int> menuAddAfter(int targetId,string newName, Result<int> res)
        {
            return menuService.menuAddAfter(targetId, newName, res);
        }

        public Result<int> menuAddBefore(int targetId, string newName, Result<int> res)
        {
            return menuService.menuAddBefore(targetId, newName, res);
        }

        public void changeMenuChild(int changeId, int targetId, Result<bool> res)
        {
            menuService.changeMenuChild(changeId, targetId, res);
        }

        public void changeMenuBefore(int changeId, int targetId, Result<bool> res)
        {
            menuService.changeMenuBefore(changeId, targetId, res);
        }

        public void changeMenuAfter(int changeId, int targetId, Result<bool> res)
        {
            menuService.changeMenuAfter(changeId, targetId, res);
        }
        public Result<int> updateMenuNameById(int targetId, string newName, Result<int> res) {
           return menuService.updateMenuNameById(targetId, newName, res);
        }
        public void deleteMenuById(int targetId, Result<int> res)
        {
            menuService.deleteMenuById(targetId, res);
        }

        #region in_core
        public Result<List<MenuInCoreEntity>> queryMenuInCoreByMenuId(int menuId, Result<List<MenuInCoreEntity>> res) {
            return menuService.queryMenuInCoreByMenuId(menuId,res);
        }
        public Result<MenuInCoreEntity> AddMenuInCoreByMenuId(Result<MenuInCoreEntity> res)
        {
            return menuService.AddMenuInCoreByMenuId( res);
        }
        public Result<MenuInCoreEntity> updateMenuInCoreById(Result<MenuInCoreEntity> res)
        {
            return menuService.updateMenuInCoreById( res);
        }
        public Result<MenuInCoreEntity> deleteMenuInCoreById(Result<MenuInCoreEntity> res)
        {
            return menuService.deleteMenuInCoreById(res);
        }
        //
        public Result<int> changeBeforeInCore(int changeId, int targetId, Result<int> res)
        {
            return menuService.changeBeforeInCore(changeId, targetId,res);
        }
        public Result<int> changeAfterInCore(int changeId, int targetId, Result<int> res)
        {
            return menuService.changeAfterInCore(changeId, targetId, res);
        }
        public Result<int> addBeforeInCore(MenuInCoreEntity entity, int targetId, Result<int> res)
        {
            return menuService.addBeforeInCore(entity, targetId, res);
        }
        public Result<int> addAfterInCore(MenuInCoreEntity entity, int targetId, Result<int> res)
        {
            return menuService.addAfterInCore(entity, targetId, res);
        }
        #endregion
    }
}
